<template>
  <div class="shop-container">
    <div class="shop-header">
      <h2>店铺管理</h2>
    </div>
    
    <el-card class="shop-card">
      <div class="shop-status-container">
        <div class="status-display">
          <div class="status-label">当前营业状态：</div>
          <div class="status-value" :class="{ 'status-open': shopStatus === 1, 'status-closed': shopStatus === 0 }">
            {{ shopStatus === 1 ? '营业中' : '已打烊' }}
          </div>
        </div>
        
        <div class="status-control">
          <el-button 
            type="primary" 
            :class="{ 'open-button': shopStatus === 0, 'close-button': shopStatus === 1 }"
            @click="handleStatusChange"
            :loading="loading"
          >
            {{ shopStatus === 1 ? '打烊' : '营业' }}
          </el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getShopStatus, setShopStatus } from '@/api/shop'

export default {
  name: 'ShopManagement',
  data() {
    return {
      shopStatus: 0, // 0: 打烊, 1: 营业中
      loading: false
    }
  },
  created() {
    this.fetchShopStatus()
  },
  methods: {
    // 获取店铺营业状态
    fetchShopStatus() {
      this.loading = true
      getShopStatus()
        .then(res => {
          if (res.code === 1) {
            this.shopStatus = res.data
          } else {
            this.$message.error(res.msg || '获取店铺状态失败')
          }
        })
        .catch(err => {
          console.error('获取店铺状态失败', err)
          this.$message.error('获取店铺状态失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    
    // 切换店铺营业状态
    handleStatusChange() {
      const newStatus = this.shopStatus === 1 ? 0 : 1
      const statusText = newStatus === 1 ? '营业' : '打烊'
      
      this.$confirm(`确认将店铺状态切换为${statusText}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        setShopStatus(newStatus)
          .then(res => {
            if (res.code === 1) {
              this.shopStatus = newStatus
              this.$message.success(`店铺已${statusText}`)
            } else {
              this.$message.error(res.msg || `设置店铺${statusText}失败`)
            }
          })
          .catch(err => {
            console.error(`设置店铺${statusText}失败`, err)
            this.$message.error(`设置店铺${statusText}失败`)
          })
          .finally(() => {
            this.loading = false
          })
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    }
  }
}
</script>

<style scoped>
.shop-container {
  padding: 20px;
}

.shop-header {
  margin-bottom: 20px;
}

.shop-card {
  margin-bottom: 20px;
}

.shop-status-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
}

.status-display {
  display: flex;
  align-items: center;
}

.status-label {
  font-size: 16px;
  margin-right: 10px;
}

.status-value {
  font-size: 18px;
  font-weight: bold;
  padding: 8px 16px;
  border-radius: 4px;
}

.status-open {
  color: #67c23a;
  background-color: #f0f9eb;
  border: 1px solid #c2e7b0;
}

.status-closed {
  color: #f56c6c;
  background-color: #fef0f0;
  border: 1px solid #fbc4c4;
}

.status-control {
  display: flex;
  align-items: center;
}

.open-button {
  background-color: #67c23a;
  border-color: #67c23a;
}

.close-button {
  background-color: #f56c6c;
  border-color: #f56c6c;
}
</style>